package com.ukao.steward.ui.valetRunners.gathering;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.bean.BalanceBean;
import com.ukao.steward.bean.OrderInfoBean;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.dialog.BingHintDialogFragment;
import com.ukao.steward.eventbus.PayEvent;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.L;
import com.ukao.steward.util.T;
import com.ukao.steward.widget.StateButton;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * 现金
 */
public class CashFragment extends BaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.pay_amount)
    TextView payAmount;
    @BindView(R.id.commit_btn)
    StateButton commitBtn;
    Unbinder unbinder;
    @BindView(R.id.layout)
    LinearLayout layout;
    @BindView(R.id.remark)
    EditText remark;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private boolean isOpen;
    private OrderInfoBean mOrderInfoBean;
    private BingHintDialogFragment mBingHintDialog;


    public static CashFragment newInstance(OrderInfoBean mOrderInfoBean, String param2) {
        CashFragment fragment = new CashFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM1, mOrderInfoBean);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mOrderInfoBean = getArguments().getParcelable(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_unionpay, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    protected void initView() {
        payAmount.setText(CheckUtils.isEmptyNumber(mOrderInfoBean.getShouldPrice()));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.commit_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.commit_btn:
                if ( commitBtn.getText().equals("确定收款")){
                    confirmPay(mOrderInfoBean.getOrderId(), mOrderInfoBean.getUseBalance());
                }

                break;
        }
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
            if (mOrderInfoBean.isbundle()) {
             unlockOrder(mOrderInfoBean.getOrderId());
        }


    }



    /*支付解锁*/
    public void unlockOrder(String orderId) {
        commitBtn.setText("订单生成中...");
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId", orderId);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores().unlockOrder(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode() == 200) {

                            commitBtn.setText("确定收款");

                        } else {

                            T.show(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {
                    dismissProgressDialog();
                    }

                });
    }


    /**
     * 确认付款（余额|余额+现金）方式
     */
    public void confirmPay(String orderId, int balance) {
        showProgressDialog();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId", orderId);
        mHashMap.put("remark", getText(remark));
        if(mOrderInfoBean.isUseUPay()){
            mHashMap.put("groupCardType","41");//groupCardType 支付(团)卡类型 41-优付卡
        }else{
            mHashMap.put("balance", String.valueOf(balance));
        }
        if (!CheckUtils.isEmpty(mOrderInfoBean.getCpnRelId())){
            mHashMap.put("cpnRelId", mOrderInfoBean.getCpnRelId());
        }

        mHashMap.put("accessToken", SaveParams.getToken());
        L.i("mHashMa=" + mHashMap.toString());
        addSubscription(apiStores().confirmPay(Constant.createParameter(mHashMap)),
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode() == 200) {
                                dismissProgressDialog();
                                 mOrderInfoBean.setPayFinish(true);
                                PayEvent.postNoData(PayEvent.Message.PAY_SUCCEED);


                        } else {
                            T.show(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }

                });
    }
}
